// message.js
/*
* 使用element-ui中Message，Loading
*
* */
import { Message, Loading } from 'element-ui'
/*
* 定义一个变量
* */
let messageInstance = null
/*
* 重置Message
* */
const resetMessage = options => {
  /*
  * messageInstance实例存在
  * 将其关闭
  * */
  if (messageInstance) {
    messageInstance.close()
  }
  messageInstance = Message(options)
}
/*
* 判断Message类型
* error：错误
* success：成功
* info：默认
* warning：警告
* */
['error', 'success', 'info', 'warning'].forEach(type => {
  resetMessage[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options
      }
    }
    options.type = type
    return resetMessage(options)
  }
})
export const message = resetMessage
/*
* 定义全局Loading
* */
export class GlobalLoading {
  int = 0;
  /*
  * 设置loading显示
  * */
  show() {
    if (this.int <= 0) {
    this.loading = Loading.service({
        lock: true,
        text: '正在处理,请稍后',
        spinner: 'el-icon-loading'
      })
    }
    this.int++
  }
  /*
  * 设置loading隐藏
  * */
  hide() {
    this.int--
    if (this.int <= 0) {
      this.loading.close()
    }
  }
}
